﻿using MaterialDesignThemes.Wpf;
using MyFirstWPF.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MyFirstWPF.View.Compnent
{
    /// <summary>
    /// AddTask.xaml 的交互逻辑
    /// </summary>
    public partial class AddTask : UserControl
    {
        // 页面关系的parent
        private Page1 MyParent;
        public AddTask(Page1 parent)
        {
            InitializeComponent();
            MyParent = parent;

            // 初始化combBox的内容
            ComboBoxItem item1 = new ComboBoxItem();
            item1.Content = "普通任务";
            cbTaskLevel.Items.Add(item1);
            ComboBoxItem item2 = new ComboBoxItem();
            item2.Content = "紧急任务";
            cbTaskLevel.Items.Add(item2);
        }

        //卡片新建提交键
        private void BtnAccept_Click(object sender, RoutedEventArgs e)
        {
            // 校验输入是否完整 或者检验是否非法
            if (tbTaskName.Text == "" || cbTaskLevel.SelectedIndex == -1||dpStartDate.Text == ""||dpEndDate.Text == ""
                ||tpStartTime.Text == "" || tpEndTime.Text == "")
            {
                StackPanel st = new StackPanel();
                st.Margin = new Thickness(16);
                TextBlock tb = new TextBlock();
                tb.FontSize = 20;
                tb.Text = "还有未填写的字段";
                st.Children.Add(tb);
                Button bt = new Button();
                bt.Content = "ok";
                bt.Click += (oo, ee) => {
                    DialogHost.CloseDialogCommand.Execute(null, null);
                };
                st.Children.Add(bt);
                DialogHost.OpenDialogCommand.Execute(st, null);
                return;
            }
            else
            {
                int minTime = DateTime.Parse(tpStartTime.Text).TimeOfDay.CompareTo(DateTime.Now.TimeOfDay);
                int minDate = DateTime.Parse(dpStartDate.Text).CompareTo(DateTime.Now.Date);
                int maxTime = DateTime.Parse(tpEndTime.Text).TimeOfDay.CompareTo(DateTime.Parse(tpStartTime.Text).TimeOfDay);
                int maxDate = DateTime.Parse(dpEndDate.Text).TimeOfDay.CompareTo(DateTime.Parse(dpStartDate.Text).TimeOfDay);
                TimeSpan timeSpan = DateTime.Parse(dpEndDate.Text).Date - DateTime.Parse(dpStartDate.Text).Date +
                                    DateTime.Parse(tpEndTime.Text).TimeOfDay - DateTime.Parse(tpStartTime.Text).TimeOfDay;
                if (minDate == -1 || (minDate == 0 && minTime == -1) ||
                maxDate == -1 || (maxDate == 0 && maxTime == -1) ||
                timeSpan.TotalHours > 24)
                {
                    StackPanel st = new StackPanel();
                    st.Margin = new Thickness(16);
                    TextBlock tb = new TextBlock();
                    tb.FontSize = 20;
                    tb.Text = "设置的时间是非法的";
                    st.Children.Add(tb);
                    Button bt = new Button();
                    bt.Content = "ok";
                    bt.Click += (oo, ee) =>
                    {
                        DialogHost.CloseDialogCommand.Execute(null, null);
                    };
                    st.Children.Add(bt);
                    DialogHost.OpenDialogCommand.Execute(st, null);
                    return;
                }
            }

            using (var db = new StudyPlanDb("../../studyPlan.db"))
            {
                //数据存入
                TaskInfo task = new TaskInfo();
                task.Name = tbTaskName.Text;
                task.Description = tbTaskDesc.Text;
                task.userId = LoginView.myId;

                task.SetStartTime(dpStartDate.SelectedDate.GetValueOrDefault(), tpStartTime.SelectedTime.GetValueOrDefault());
                task.SetEndTime(dpEndDate.SelectedDate.GetValueOrDefault(), tpEndTime.SelectedTime.GetValueOrDefault());
                task.SetLastNoticeTime(DateTime.MinValue);
                //task.SetLastDailyNoticeTime(DateTime.MinValue);

                task.isFinished = 0;
                task.Level = (TaskInfo.TaskLevel)cbTaskLevel.SelectedIndex;
                db.Insert(task);
                MyParent.reInitCards();
            }

            clearDialogData();
            // todo 执行添加数据和卡片的操作
            DialogHost.CloseDialogCommand.Execute(null, null);
        }

        //卡片新建关闭键
        private void BtnCancel_Click(object sender, RoutedEventArgs e)
        {
            clearDialogData();
            DialogHost.CloseDialogCommand.Execute(null, null);
        }

        private void clearDialogData()
        {
            // 清空输入数据
            tbTaskName.Text = "";
            tbTaskDesc.Text = "";
            dpStartDate.SelectedDate = null;
            tpStartTime.SelectedTime = null;
            dpEndDate.SelectedDate = null;
            tpEndTime.SelectedTime = null;
            cbTaskLevel.SelectedIndex = -1;
        }
    }
}
